﻿using LogForParameter.IServices;
using LogForParameter.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using LogForParameter.Api.SwaggerHelp;
using AutoMapper;
using Library.Helper;
using Library.Common.Helper;
using ViewModel.ORDERPATROL_APILOGVM;

namespace LogForParameter.Api.Controllers
{
    [CustomRoute(CustomApiVersion.MyApiVersion.GTestDemo)]
    [ApiController]
    //[Authorize(Permissions.Name)]
    public class ORDERPATROL_APILOGController : ControllerBase
    {
        /// <summary>
        /// 服务器接口，因为是模板生成，所以首字母是大写的，自己可以重构下
        /// </summary>
        private readonly IORDERPATROL_APILOGServices _oRDERPATROL_APILOGServices;
        private readonly IMapper _mapper;

        public ORDERPATROL_APILOGController(IORDERPATROL_APILOGServices ORDERPATROL_APILOGServices, IMapper mapper)
        {
            _oRDERPATROL_APILOGServices = ORDERPATROL_APILOGServices;
            _mapper = mapper;
        }

        [HttpGet]
        public async Task<MessageModel<PageModel<ORDERPATROL_APILOG>>> Get(int page = 1, string key = "", int intPageSize = 50)
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }

            Expression<Func<ORDERPATROL_APILOG, bool>> whereExpression = a => true;

            return new MessageModel<PageModel<ORDERPATROL_APILOG>>()
            {
                msg = "获取成功",
                success = true,
                response = await _oRDERPATROL_APILOGServices.QueryPage(whereExpression, page, intPageSize)
            };

        }

        [HttpGet("{id}")]
        public async Task<MessageModel<ORDERPATROL_APILOG>> Get(string id)
        {
            return new MessageModel<ORDERPATROL_APILOG>()
            {
                msg = "获取成功",
                success = true,
                response = await _oRDERPATROL_APILOGServices.QueryById(id)
            };
        }

        [HttpPost]
        public async Task<MessageModel<string>> Post([FromBody] ORDERPATROL_APILOGAddModels request)
        {
            var data = new MessageModel<string>();
            var entity = _mapper.Map<ORDERPATROL_APILOG>(request);
            var id = await _oRDERPATROL_APILOGServices.Add(entity);
            if (id > 0)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
                data.success = true;
            }
            return data;
        }

        [HttpPut]
        public async Task<MessageModel<string>> Put([FromBody] ORDERPATROL_APILOG request)
        {
            var data = new MessageModel<string>();
            data.success = await _oRDERPATROL_APILOGServices.Update(request);
            if (data.success)
            {
                data.msg = "更新成功";
                data.response = request?.ID.ObjToString();
            }

            return data;
        }

        [HttpDelete("{id}")]
        public async Task<MessageModel<string>> Delete(string id)
        {
            var data = new MessageModel<string>();
            data.success = await _oRDERPATROL_APILOGServices.DeleteById(id);
            if (data.success)
            {
                data.msg = "删除成功";
                data.response = id;
            }

            return data;
        }
        /// <summary>
        /// sqlsugar方法测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<string>> BaseQuaryTest()
        {
            var data = MessageModel<string>.OK();
            data.response= await _oRDERPATROL_APILOGServices.BaseQuaryTest();
            return data;
        }
    }
}